package com.stars.home.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.base.MBActivity
import com.stars.home.databinding.ActivityChoseTimeBinding
import com.stars.home.inte.ChoseTimeData
import com.stars.home.utils.ChoseMileTime
import com.stars.home.utils.time.Constant
import com.stars.home.utils.time.DateUtils
import com.stars.home.view.dateView.CalendarView
import com.stars.home.view.dateView.CalendarView.CalendaSelListener
import com.stars.home.vmodel.ChoseTimeVModel
import java.util.*

/**
 * 选择日期
 *on 2021/5/8
 */
class ChoseTimeActivity: MBActivity<ChoseTimeVModel, ActivityChoseTimeBinding>() {

    private var starTime: String? = null
    private var endTime: String? = null
    private var isSelecgOk = false

    override fun listeners() {
        super.listeners()

        binding.calendarview.setSTimeSelListener(object : CalendarView.CalendarSTimeSelListener {
            override fun onSTimeSelect(date: Date?) {

                if (date != null) {
                    starTime = DateUtils.formatData(date, Constant.TFORMATE_YMD);
                    binding.tvStartime.setText(starTime);
                } else {
                    starTime = null
                }

            }

        })

        binding.calendarview.setETimeSelListener(object : CalendarView.CalendatEtimSelListener {
            override fun onETimeSelect(date: Date?) {

                if (date != null) {
                    endTime = DateUtils.formatData(date, Constant.TFORMATE_YMD);
                    binding.tvEndtime.text = endTime
                } else {
                    endTime = null
                }

            }

        })


        binding.calendarview.setCalendaSelListener(CalendaSelListener { isOk -> isSelecgOk = isOk })



        binding.llChoseTime.onClick {
            //选择时间

            ChoseMileTime.show(context(),vm.star_time.value,vm.end_time.value,binding.llChoseTime,object : ChoseTimeData {
                override fun onRestuse(day: String?, dayTo: String?) {
                    vm.star_time.value = day
                    vm.end_time.value= dayTo

                }
            })



        }

        binding.llPush.onClick {

            var dayStart=binding.tvStartime.text.toString()
            var dayEnd=binding.tvEndtime.text.toString()

            if(TextUtils.isEmpty(dayStart)){

                toast("请选择开始日期")
                return@onClick
            }

            var timeStart=binding.tvStart.text.toString()
            var timeEnd=binding.tvEnd.text.toString()
            if(TextUtils.isEmpty(timeStart)){
                toast("请选择时间段")
                return@onClick
            }

            val intent = Intent()
            var bundle= Bundle()
            bundle.putString("startDay", "$dayStart $timeStart")
            bundle.putString("endDay", "$dayEnd $timeEnd")
            intent.putExtras(bundle)
            setResult(Activity.RESULT_OK, intent)
            finish()


        }

    }


    override fun vmClass(): Class<ChoseTimeVModel> {
        return ChoseTimeVModel::class.java
    }
    override fun binding(): ActivityChoseTimeBinding {
       return ActivityChoseTimeBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return "日期选择"
    }
}
